package com.hykj.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hykj.toolsbean.DB;
import com.hykj.valuebean.news;
import com.hykj.valuebean.products;

public class NewsDao extends SuperDao {

	private DB mydb = null;

	public NewsDao() {
		mydb = new DB();
	}

	public List getAllNews(String strcurrentP, String strcurrentG,
			String goWhich) throws SQLException {
		String sqlall = "select * from news order by date desc";
		setDaoPage(sqlall, null, strcurrentP, strcurrentG, goWhich);
		int currentP = getDaoPage().getCurrentP();
		int top1 = getDaoPage().getPerR();
		int top2 = (currentP - 1) * top1;

		String sqlsub = "";
		if (currentP == 1)
			sqlsub = "select * from news order by date desc limit " + top1 + "";
		else
			sqlsub = "select * from news where (date < (select min(date) from (select * from news order by date desc limit "
					+ top2
					+ ") as minv)) order by date desc limit "
					+ top1
					+ "";

		List alllist = getList(sqlsub, null);
		return alllist;
	}

	public news getSingleNew(int id) {
		news single = null;
		String sql = "select * from news where id=?";
		Object[] params = { id };
		try {
			List list = getList(sql, params);
			if (list != null && list.size() != 0)
				single = (news) list.get(0);
			return single;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public void updateNews(news newsIns) {
		String sql = "update news set title = ?, date = ?, author = ?, original = ?, article = ?, category = ? where id = ?";
		Object[] params = { newsIns.getTitle(), newsIns.getDate(),
				newsIns.getAuthor(), newsIns.getOriginal(),
				newsIns.getArticle(), newsIns.getCategory(), newsIns.getId() };
		mydb.doPstm(sql, params);
	}

	public void insertNews(news newsIns) {
		String sql = "insert news (title, date, author, original, article, category) values (?, ?, ?, ?, ?, ?)";
		Object[] params = { newsIns.getTitle(), newsIns.getDate(),
				newsIns.getAuthor(), newsIns.getOriginal(),
				newsIns.getArticle(), newsIns.getCategory() };
		mydb.doPstm(sql, params);
	}

	public void deleteNew(int id) {
		String sql = "delete from news where id =?";
		Object[] params = { id };
		mydb.doPstm(sql, params);
	}
	
	public void deleteNew(String url) {
		String sql = "delete from news where id in" + url;
		Object[] params = null;
		mydb.doPstm(sql, params);
	}


	public List getCategoryNews(String strcurrentP, String strcurrentG,
			String goWhich, String category) throws SQLException {
		String sqlall = "select * from news where category = ? order by date desc";
		Object[] params = { category };
		Object[] par = { category, category };
		setDaoPage(sqlall, params, strcurrentP, strcurrentG, goWhich);
		int currentP = getDaoPage().getCurrentP();
		int top1 = getDaoPage().getPerR();
		int top2 = (currentP - 1) * top1;

		String sqlsub = "";
		if (currentP == 1) {
			sqlsub = "select * from news where category = ? order by id desc limit "
					+ top1 + "";

		} else {

			sqlsub = "select * from news where (id < (select min(id) from (select * from news where category = ? order by id desc limit "
					+ top2
					+ ") as minv)) and category = ? order by id desc limit "
					+ top1 + "";

		}

		if (currentP == 1) {
			List alllist = getList(sqlsub, params);

			return alllist;
		} else {
			List alllist = getList(sqlsub, par);

			return alllist;
		}

	}

	private List getList(String sql, Object[] params) throws SQLException {
		List list = null;
		mydb.doPstm(sql, params);
		ResultSet rs = mydb.getRs();
		if (rs != null) {
			list = new ArrayList();
			while (rs.next()) {
				news single = new news();
				single.setId(rs.getInt(1));
				single.setTitle(rs.getString(2));
				single.setDate(rs.getString(3));
				single.setAuthor(rs.getString(4));
				single.setOriginal(rs.getString(5));
				single.setArticle(rs.getString(6));
				single.setCategory(rs.getString(7));

				list.add(single);
			}
			rs.close();
		}
		return list;
	}

	public List<news> getNews() {
		List<news> list = null;
		String sql = "select * from news limit 8";
		Object[] params = null;
		try {
			mydb.doPstm(sql, params);
			ResultSet rs = mydb.getRs();
			if (rs != null) {
				list = new ArrayList<news>();
				news single;
				while (rs.next()) {
					single = new news();
					single.setId(rs.getInt(1));

					single.setTitle(rs.getString(2));

					list.add(single);
				}
				rs.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public void closed() {
		mydb.closed();
	}

}
